#include "setserialparam.h"

SetSerialParam::SetSerialParam(QWidget *parent,SERIAL_SET_PARAM *p) : QWidget(parent)
{
    _serial_set_param = p;
    //波特率
    QStringList serial_baud_str;

    serial_baud_str<<"1200"<<"2400"<<"4800"<<"9600"<<"19200"<<"38400"<<"57600"<<"115200";
    serial_baud_combox = new QComboBox(this);
    serial_baud_combox->addItems(serial_baud_str);

//serial->setBaudRate(QSerialPort::Baud115200);
    //停止位
    QStringList serial_stop_bit_str;
    serial_stop_bit_str<<"1"<<"1.5"<<"2";
    serial_stop_bit_combox = new QComboBox(this);
    serial_stop_bit_combox->addItems(serial_stop_bit_str);


    //数据位
    QStringList serial_data_bit_str;
    serial_data_bit_str <<"5"<<"6"<<"7"<<"8";
    serial_data_bit_combox = new QComboBox(this);
    serial_data_bit_combox->addItems(serial_data_bit_str);


    //奇偶检验
    QStringList serial_odd_even_check_bit_str;
    serial_odd_even_check_bit_str<<"无"<<"奇校验"<<"偶检验";
    serial_odd_even_check_combox = new QComboBox(this);
    serial_odd_even_check_combox->addItems(serial_odd_even_check_bit_str);

    serial_port_combox = new QComboBox(this);
    serial_port_combox->addItems(get_enable_serial_port());

    serial_port_combox          ->resize(100,30);
    serial_baud_combox          ->resize(100,30);
    serial_stop_bit_combox      ->resize(100,30);
    serial_data_bit_combox      ->resize(100,30);
    serial_odd_even_check_combox->resize(100,30);

    QLabel *serialSelectPortLabel = new QLabel(this);
    serialSelectPortLabel->setText("串口选择:");
    serialSelectPortLabel->resize(80,30);
    serialSelectPortLabel->move(20,20);

    QLabel *serialSelectBaudLabel = new QLabel(this);
    serialSelectBaudLabel->setText("波特率:");
    serialSelectBaudLabel->resize(80,30);
    serialSelectBaudLabel->move(20,80);


    QLabel *serialStopBitLabel = new QLabel(this);
    serialStopBitLabel->setText("停止位:");
    serialStopBitLabel->resize(80,30);
    serialStopBitLabel->move(20,140);


    QLabel *serialDataBitLabel = new QLabel(this);
    serialDataBitLabel->setText("数据位:");
    serialDataBitLabel->resize(80,30);
    serialDataBitLabel->move(20,200);


    QLabel *serialOddEvenCheckLabel = new QLabel(this);
    serialOddEvenCheckLabel->setText("奇偶校验:");
    serialOddEvenCheckLabel->resize(80,30);
    serialOddEvenCheckLabel->move(20,260);


    serial_port_combox          ->move(130,20);
    serial_baud_combox          ->move(130,80);
    serial_stop_bit_combox      ->move(130,140);
    serial_data_bit_combox      ->move(130,200);
    serial_odd_even_check_combox->move(130,260);


    serial_baud_combox          ->setCurrentIndex(7);
    serial_stop_bit_combox      ->setCurrentIndex(0);
    serial_data_bit_combox      ->setCurrentIndex(3);
    serial_odd_even_check_combox->setCurrentIndex(0);



    serial_open_btn = new QPushButton(this);
    serial_open_btn->move(130,400);
    serial_open_btn->setText("打开串口");
    serial_open = false;
    connect(serial_open_btn,SIGNAL(released()),this,SLOT(serial_open_btn_handle()),Qt::AutoConnection);//

}

//获取串口设置参数
void SetSerialParam:: get_serial_param()
{
    //获取波特率参数
    switch (serial_baud_combox->currentIndex())
    {

        case 0:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud1200;
            break;
        case 1:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud2400;
            break;
        case 2:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud4800;
            break;
        case 3:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud9600;
            break;
        case 4:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud19200;
            break;
        case 5:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud38400;
            break;
        case 6:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud57600;
            break;
        case 7:
            _serial_set_param->m_serial_param_baud = QSerialPort::Baud115200;
            break;
        default:
            break;
    }
    //停止位设置
    switch (serial_stop_bit_combox->currentIndex())
    {
        case  0:
            _serial_set_param->m_serial_param_stop_bit = QSerialPort::OneStop;
            break;
        case  1:
            _serial_set_param->m_serial_param_stop_bit = QSerialPort::OneAndHalfStop;
            break;
        case  2:
            _serial_set_param->m_serial_param_stop_bit = QSerialPort::TwoStop;
            break;

        default:
            break;
    }
    //数据位
    switch (serial_data_bit_combox->currentIndex())
    {
        case 0:
            _serial_set_param->m_serial_param_data_bit = QSerialPort::Data5;
            break;
        case 1:
            _serial_set_param->m_serial_param_data_bit = QSerialPort::Data6;
            break;
        case 2:
            _serial_set_param->m_serial_param_data_bit = QSerialPort::Data7;
            break;
        case 3:
            _serial_set_param->m_serial_param_data_bit = QSerialPort::Data8;
            break;
        default:
            break;
    }
    //校验位
    switch (serial_odd_even_check_combox->currentIndex())
    {
        case 0:
            _serial_set_param->m_serial_param_check_bit = QSerialPort::NoParity;
            break;
        case 1:
            _serial_set_param->m_serial_param_check_bit = QSerialPort::EvenParity;
            break;
        case 2:
            _serial_set_param->m_serial_param_check_bit = QSerialPort::OddParity;
            break;

        default:
            break;
    }
    _serial_set_param->serial_port_name = serial_port_combox->currentText();
}







// 获取计算机可用串口
QStringList SetSerialParam::get_enable_serial_port()
{
    QStringList CommPortList;

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        if (serial.open(QIODevice::ReadWrite))
        {
            CommPortList.append(serial.portName());
            serial.close();
        }
    }

    return CommPortList;
}





void SetSerialParam::serial_open_btn_handle()
{
    if(serial_open == false)
    {
        serial_open = true;
        serial_open_btn->setText("关闭串口");
        emit serial_open_signal();
    }
    else
    {
        serial_open = false;
        serial_open_btn->setText("打开串口");
        emit serial_close_signal();
    }
}
















